# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        slow = head
        fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        half = slow

        pre = None
        cur = half
        while cur:
            nex = cur.next
            cur.next = pre
            pre = cur
            cur = nex

        start = head
        while pre and start != pre:
            if start.val != pre.val:
                return False
            start = start.next
            pre = pre.next
        return True





